package the.workshop.milktea.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.workshop.milktea.infrastructure.mapper.IngredientMapper;
import the.workshop.milktea.infrastructure.model.Ingredient;
import the.workshop.milktea.interfaces.resp.IngredientPageResp;
import the.workshop.milktea.service.dto.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 奶茶原料 服务实现类
 * </p>
 *
 * @author huamu
 * @since 2023-04-01
 */
@Service
public class IngredientService extends AbstractSuperService<IngredientMapper, Ingredient> {

    public boolean add(IngredientAddDTO dto) {
        Ingredient entity = new Ingredient();
        BeanUtils.copyProperties(dto, entity);

        entity.setCreatedId(dto.getOperatorId());
        entity.setCreatedName(dto.getOperatorName());
        entity.setCreatedTime(new Date());
        entity.setModifiedId(dto.getOperatorId());
        entity.setModifiedName(dto.getOperatorName());
        entity.setModifiedTime(new Date());
        this.baseMapper.insert(entity);
        return true;
    }

    public boolean modify(IngredientModifyDTO dto) {
        Ingredient entity = new Ingredient();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(dto.getIngredientId());

        entity.setModifiedId(dto.getOperatorId());
        entity.setModifiedName(dto.getOperatorName());
        entity.setModifiedTime(new Date());
        this.baseMapper.updateById(entity);
        return true;
    }

    public QueryResult<IngredientPageResultDTO> page(IngredientPageDTO condition) {
        if (condition.getPageNum() == null || condition.getPageSize() == null) {
            return null;
        }
        PageHelper.startPage(condition.getPageNum(), condition.getPageSize());
        Page<IngredientPageResultDTO> page = this.baseMapper.selectListPage(condition);
        return new QueryResult(page.getTotal(), page.getResult());
    }

    public IngredientGetDTO get(Integer id) {
        Ingredient entity = this.baseMapper.selectById(id);
        if (entity == null) {
            return null;
        }
        IngredientGetDTO dto = new IngredientGetDTO();
        BeanUtils.copyProperties(entity, dto);
        dto.setIngredientId(entity.getId());
        return dto;
    }

    public List<IngredientPageResp> getList() {
        List<Ingredient> ingredientList = this.baseMapper.selectList(new LambdaQueryWrapper<>());
        return CollectionUtils.isEmpty(ingredientList) ? null : ingredientList.stream().map(this::convertOf).collect(Collectors.toList());
    }

    private IngredientPageResp convertOf(Ingredient source) {
        IngredientPageResp target = new IngredientPageResp();
        BeanUtils.copyProperties(source, target);
        target.setIngredientId(source.getId());
        return target;
    }
}
